<?php
  $content = '';
  # Check if user may edit this page                 
  $edit = $_SESSION['logged'] && $_SESSION['rights'] & $r_webmaster;
  
  if ($_POST['submitted']) {
    extract($_POST);
    
    /**
    * Existing Offices
    */
    if ($res=mq("SELECT OID FROM officers")) {
        while($data = mysql_fetch_array($res)) {
            $OID=$data[0];
            mq("UPDATE officers SET ".($_POST['MID_'.$OID] ? "MID=".$_POST['MID_'.$OID].",":'')." eMail='".$_POST['eMail_'.$OID]."',mobile='".$_POST['mobile_'.$OID]."' WHERE OID=".$OID);
        }
    }
    
    /**
    * New Office
    */
    if ($newoffice) {
        mq("INSERT INTO officers (Office) VALUES ('$newoffice')");
        logevent("New Office: ".$newoffice);
    }
  } else if ($edit && $_GET['delete'] && $d = mqs("SELECT OID,Office FROM officers WHERE OID=".($_GET['delete']+0))) {
    mq("DELETE FROM officers WHERE OID=".$d['OID']);
    logevent("Office \'".$d['Office']."\' deleted.");
  }
  
  if ($res = mq("SELECT OID,Office,o.eMail,mobile,Name,m.MID,Pic FROM officers o
                 ".($_SESSION['logged'] ? 'LEFT OUTER':'INNER')." JOIN members m ON m.MID=o.MID
                 ORDER BY Office")) {
    include('data/lib_f_pictures.inc.php');
    include('data/v_global.inc.php'); # Override standard values
    
    if ($edit) {
        include('data/lib_components.inc.php');
        $content .= '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">';
    }
    $content .= '<script type="text/javascript">
                    function ShowPic(file) {
                       pic = window.open(file,"Bildansicht","location=no,scrollbars=no,menu=no,dependent=yes,status=no");
                       pic.focus();
                    }
                 </script>
                 <table cellpadding="3">';  
    while($data = mysql_fetch_array($res)) {
        extract($data);
        $content .= '<tr>
                       <td rowspan="4">'.getShowPic(MEMBERPICDIR.$Pic,$thumbwidth,$thumbheight,$Name).'</td>
                       <td><b>'.$Office.'</b></td>
                     </tr>
                     <tr><td>'.($edit ? selectfromquery('MID_'.$OID,"SELECT MID,Name FROM members ORDER BY Name",$MID,"Please select a member")
                                        .'<a href="'.$_SERVER['PHP_SELF'].'?delete='.$OID.'" onclick="return confirm(\'Delete Office?\nThis cannot be undone and will destroy all\nlinks to this office made within other pages!\')">
                                            <img src="'.BILDERDIR.'del.gif" alt="del">
                                           </a>'
                                      :$Name).'</td></tr>
                     <tr><td>'.($edit ? '<input type="text" class="text" name="eMail_'.$OID.'" value="'.$eMail.'" size="35"> eMail':$eMail).'</td></tr>
                     <tr><td>'.($edit ? '<input type="text" class="text" name="mobile_'.$OID.'" value="'.$mobile.'" size="35"> Phone':$mobile).'</td></tr>
                     <tr><td colspan="2">&nbsp</td></tr>';
    }
    $content .= ($edit) ? '<tr><td colspan="2">Enter new office (optional):</td></tr>
                 <tr><td colspan="2"><input type="text" class="text" name="newoffice" size="40"></td></tr>
                 <tr><td colspan="2"><input type="submit" class="button" value="submit"></td></tr>
                 </table>
                 <input type="hidden" name="submitted" value="1">
                 </form>':'</table>';
  } else $content = 'no officers yet.';
?>